package com.travel.server.manager.impl.command.message;

import com.travel.common.CommonConstant;
import com.travel.server.manager.dto.request.message.DeleteMessageRequest;
import com.travel.server.manager.dto.response.EmptyResponse;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.MessageDal;
import com.travel.server.manager.impl.enums.MessageTemplateEnum;
import com.travel.server.manager.impl.sms.SmsSal;
import com.travel.server.manager.model.MessageTemplate;
import com.travel.server.manager.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author: LiZhenhui
 * @Date: 2018\11\15 0015 22:19
 * @Desc:
 */
@Component
@Slf4j
public class DeleteMessageCommand extends AbstractCommand<DeleteMessageRequest, EmptyResponse> {

    @Autowired
    private MessageDal messageDal;

    @Override
    protected void checkInput(DeleteMessageRequest request) {
        if(request.getId() == null || request.getType() == null){
            throwInvalidParam();
        }
    }

    @Override
    public EmptyResponse handle(DeleteMessageRequest request) {
        checkInput(request);
        UserInfo userInfo = getUserInfo();

        MessageTemplate messageTemplate = messageDal.selectById(request.getId());
        if(MessageTemplateEnum.MSG.getType().equals(messageTemplate.getMessageType())){
            SmsSal.deleteSms(messageTemplate.getSmsTemplateId());
        }
        messageTemplate.setState(CommonConstant.NO);
        messageTemplate.setUpdateBy(userInfo.getName());
        messageTemplate.setUpdateTime(new Date());
        messageDal.inserOrUpdate(messageTemplate);
        return new EmptyResponse();
    }
}
